package com.jianyun.jyzs.model;

import android.util.Log;

import com.jianyun.jyzs.bean.AnnouncementList;
import com.jianyun.jyzs.dao.AnnouncementDao;
import com.jianyun.jyzs.http.Api;
import com.jianyun.jyzs.http.CustomCallback;
import com.jianyun.jyzs.http.RetrofitHelper;
import com.jianyun.jyzs.model.imdoel.IAnnouncementsModel;

import java.util.HashMap;
import java.util.List;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 获取通知公告列表
 * Created by yangshijun on 2018/5/29.
 */

public class AnnouncementModel implements IAnnouncementsModel {

    private static AnnouncementModel model;

    private AnnouncementModel() {
    }

    public static AnnouncementModel getInstance() {
        if (model == null) {
            model = new AnnouncementModel();
        }
        return model;
    }

    /**
     * 获取通知公告列表
     *
     * @param map
     * @param onGetAnnoucemntListener
     */
    @Override
    public void getAnnouncementList(final AnnouncementDao dao, final HashMap<String, String> map, final OnGetAnnoucemntListener onGetAnnoucemntListener) {
        /*Api api = RetrofitHelper.builderbch().create(Api.class);
        api.getAnnouncementList(map).observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new CustomCallback<AnnouncementList>() {
                    @Override
                    protected void onResponse(AnnouncementList response) {
                        if (response.isResult()) {
                            List<AnnouncementList.Announcement> list = response.getList();
                            for (AnnouncementList.Announcement aa : list) {
                                aa.setIntId(Integer.parseInt(aa.getId()));
                                dao.insert(aa, map.get("enterpriseCode"));
                            }
                            onGetAnnoucemntListener.onSuccess(list.size());
                        } else {
                            //获取数据失败
                            onGetAnnoucemntListener.onFaile(response.getMessage());
                        }
                    }

                    @Override
                    protected void onException(String msg) {
                        //获取数据失败
                        onGetAnnoucemntListener.onFaile(msg);
                    }
                });*/
    }

    /**
     * 新的获取通知公告列表
     *
     * @param PageIndex
     * @param PageSize
     */
    public void getAnnouncement(final AnnouncementDao dao, final String enterpriseCode, String userId,int PageIndex, int PageSize, final OnGetAnnoucemntListener onGetAnnoucemntListener) {
        Api api = RetrofitHelper.builderErpRoot().create(Api.class);
        api.getAnnouncement("GetNotes",enterpriseCode,userId, PageIndex, PageSize,"jyzs","Android","1.0.2")
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new CustomCallback<AnnouncementList>() {
                    @Override
                    protected void onResponse(AnnouncementList response) {
                        Log.i("test", "通知：" + response.toString());
                        if (response.isResult()) {
                            try {
                                List<AnnouncementList.Announcement> list = response.getList();
                                for (AnnouncementList.Announcement aa : list) {
                                    aa.setIntId(Integer.parseInt(aa.getId()));
                                    dao.insert(aa, enterpriseCode);
                                }
                                onGetAnnoucemntListener.onSuccess(list.size());
                            }catch (Exception e){
                                //获取数据失败
                                onGetAnnoucemntListener.onFaile(response.getMessage());
                            }

                        } else {
                            //获取数据失败
                            onGetAnnoucemntListener.onFaile(response.getMessage());
                        }
                    }

                    @Override
                    protected void onException(String msg) {
                        //获取数据失败
                         onGetAnnoucemntListener.onFaile(msg);
                    }
                });
    }
}
